package org.bjtu.sinet.flowrule;

import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.onosproject.cfg.ComponentConfigService;
import org.onosproject.net.pinet.PINetNetworkService;
import org.onosproject.net.topology.Topology;
import org.onosproject.net.topology.TopologyService;
import org.onosproject.rest.AbstractWebResource;
import org.onosproject.util.PINetType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.io.InputStream;
import java.util.Set;

@Path("/")
public final class PerformanceTestResourcer extends AbstractWebResource {
    private final Logger log = LoggerFactory.getLogger(getClass());

    private static class NetworkConfig {
        private String routingStrategy = "distance"; // 初始为距离优先
        private double gridSize = 2.0; // 初始为2米

        public String getRoutingStrategy() {
            return routingStrategy;
        }

        public void setRoutingStrategy(String routingStrategy) {
            this.routingStrategy = routingStrategy;
        }

        public double getGridSize() {
            return gridSize;
        }

        public void setGridSize(double gridSize) {
            this.gridSize = gridSize;
        }
    }

    private final NetworkConfig networkConfig = new NetworkConfig();

    @GET
    @Path("/test")
    public Response getTest() {
        TopologyService topolog = get(TopologyService.class);
        Topology topology = topolog.currentTopology();
        System.out.println(topolog.toString());
        ObjectNode responseBody = new ObjectNode(JsonNodeFactory.instance);
        responseBody.put("message", "it works!");
        return Response.status(200).entity(responseBody).build();
    }

    @GET
    @Path("/test/pinet")
    public Response getTestPinet() {
        PINetNetworkService topolog = get(PINetNetworkService.class);
        topolog.getPINetNetworks(PINetType.GEOCODE);
        System.out.println(topolog.toString());
        ObjectNode responseBody = new ObjectNode(JsonNodeFactory.instance);
        responseBody.put("message", "it works!");
        return Response.status(200).entity(responseBody).build();
    }

    @POST
    @Path("/performance/geo/test")
    @Produces(MediaType.APPLICATION_JSON)
    public Response startPerformanceTest(
            InputStream stream,
            @QueryParam(value = "source") String source,
            @QueryParam(value = "destination")String destination,
            @QueryParam(value = "packetSize")int packetSize,
            @QueryParam(value = "duration")int duration

    ) {
        log.info("testting ");
        ComponentConfigService service = get(ComponentConfigService.class);
        Set<String> components = service.getComponentNames();
        components.forEach(System.out::println);
        ObjectNode responseBody = new ObjectNode(JsonNodeFactory.instance);
        responseBody.put("message", "testting is ok");
        return Response.status(200).entity(responseBody).build();
    }


    @POST
    @Path("/routing")
    //@Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Response geoStrategy(
            InputStream stream,
            @QueryParam(value = "strategy")String strategy

    ) {
        ObjectNode responseBody = new ObjectNode(JsonNodeFactory.instance);
        if ("distance".equals(strategy) || "delay".equals(strategy)) {
            networkConfig.setRoutingStrategy(strategy);
            responseBody.put("message", "Routing strategy updated to: " + strategy);
        } else {
            responseBody.put("message", "Invalid routing strategy: " + strategy);
        }
        log.info("testting ");


        return Response.status(200).entity(responseBody).build();
    }

    @POST
    @Path("/grid-size")
    //@Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Response startPerformanceTest(
            InputStream stream,
            @QueryParam(value = "size") Double size

    ) {
        ObjectNode responseBody = new ObjectNode(JsonNodeFactory.instance);
        if (size == 2.0 || size == 1.0 || size == 0.5) {
            networkConfig.setGridSize(size);
            responseBody.put("message", "Grid size updated to: " + size + "m");
        } else {
            responseBody.put("message", "Invalid grid size: " + size);
        }
        log.info("grid-size ");


        return Response.status(200).entity(responseBody).build();
    }


}
